{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "is_executing": true
    },
    "ExecuteTime": {
     "start_time": "2024-11-24T06:00:47.464994Z"
    }
   },
   "source": [
    "import codecs\n",
    "\n",
    "import os\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "from gensim import corpora\n",
    "from gensim.models import LdaModel\n",
    "from gensim.corpora import Dictionary\n",
    "\n",
    "import pyLDAvis.gensim\n",
    "import pyLDAvis\n",
    "import pyLDAvis.gensim_models as gensimvis\n",
    "from gensim.models import LdaModel\n",
    "from gensim.corpora import Dictionary\n",
    "\n",
    "train = []\n",
    "\n",
    "fp = codecs.open('D:\\\\pycharmproject\\\\高校图书采购主题分析\\\\CNKI-output.txt','r',encoding='utf8')\n",
    "for line in fp:\n",
    "    if line != '':\n",
    "        line = line.split()\n",
    "        train.append([w for w in line])\n",
    "\n",
    "dictionary = corpora.Dictionary(train)\n",
    "\n",
    "corpus = [dictionary.doc2bow(text) for text in train]\n",
    "\n",
    "lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=20, passes=60)\n",
    "# num_topics：主题数目\n",
    "# passes：训练伦次\n",
    "# num_words：每个主题下输出的term的数目\n",
    "'''\n",
    "for topic in lda.print_topics(num_words = 20):\n",
    "    termNumber = topic[0]\n",
    "    print(topic[0], ':', sep='')\n",
    "    listOfTerms = topic[1].split('+')\n",
    "    for term in listOfTerms:\n",
    "        listItems = term.split('*')\n",
    "        print('  ', listItems[1], '(', listItems[0], ')', sep='')\n",
    "\n",
    "print(type(vis_data))\n",
    "print(type(lda))\n",
    "print(type(corpus))\n",
    "print(type(dictionary))\n",
    "'''\n",
    "\n",
    "# 准备可视化数据 换成 mmds 效果更好\n",
    "vis_data = pyLDAvis.gensim.prepare(lda, corpus, dictionary, mds='mmds')\n",
    "\n",
    "#直接显示\n",
    "pyLDAvis.display(vis_data)\n",
    "\n",
    "\n",
    "# 保存为 HTML 文件\n",
    "#pyLDAvis.save_html(vis_data, 'E:\\\\allme\\\\learning\\\\reproducing-the-code-from-a-paper\\\\rcp0810-for-CNIK-Data\\\\ldavis.html')\n",
    "# 打印当前工作目录 C:\\Users\\Evie\n",
    "#print(os.getcwd())"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "33a8e0744016f85c"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
